package persistency;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;

import model.Customer;


public class CustomerRepository  implements Repository<Customer>  {

	private static final String PERSISTENCE_UNIT_NAME = "MiniServiceDesk";
	
	@Override
	public void insert(Customer entity)  throws PersistencyException {
		  EntityManager em = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT_NAME).createEntityManager(); 
		  EntityTransaction tx = em.getTransaction(); 
		  try
		  {
			  tx.begin(); 
			  em.persist(entity); 
			  em.flush();
			  tx.commit(); 
		  } catch (Exception ex) {
				if (tx != null && tx.isActive()) {
					tx.rollback();
				}
				throw new PersistencyException("Could not perform action. Rollback triggered!", ex);
		  } finally {
				if (em != null && em.isOpen()) {
					em.close();
				}
		  }		
	}

	@Override
	public void update(Customer entity) throws PersistencyException {
		EntityManager em = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT_NAME).createEntityManager();
		EntityTransaction tx = em.getTransaction();
		try {
			tx.begin();
			em.merge(entity);
			tx.commit();
		  } catch (Exception ex) {
				if (tx != null && tx.isActive()) {
					tx.rollback();
				}
				throw new PersistencyException("Could not perform action.Rollback triggered!", ex);
		  } finally {
				if (em != null && em.isOpen()) {
					em.close();
				}
		  }			
	}
	
	@Override
	public void delete(Customer entity) throws PersistencyException {
		EntityManager em = Persistence.createEntityManagerFactory(
				PERSISTENCE_UNIT_NAME).createEntityManager();
		EntityTransaction tx = em.getTransaction();
		try {
			tx.begin();
			Customer dObject = getById(entity.getCustomerId());
			em.remove(dObject);
			tx.commit();
		  } catch (Exception ex) {
				if (tx != null && tx.isActive()) {
					tx.rollback();
				}
				throw new PersistencyException("Could not perform action.Rollback triggered!", ex);
		  } finally {
				if (em != null && em.isOpen()) {
					em.close();
				}
		  }	
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Customer> getAll() throws PersistencyException {
		EntityManager em = Persistence.createEntityManagerFactory(
				PERSISTENCE_UNIT_NAME).createEntityManager();
		EntityTransaction tx = em.getTransaction();
		try {
			tx.begin();
			List<Customer> dObjects = em.createQuery("Select cust from Customer cust").getResultList();			
			tx.commit();
			return dObjects;
		  } catch (Exception ex) {
				if (tx != null && tx.isActive()) {
					tx.rollback();
				}
				throw new PersistencyException("Could not perform action.Rollback triggered!", ex);
		  } finally {
				if (em != null && em.isOpen()) {
					em.close();
				}
		  }
	}
	
	@Override
	public Customer getById(Object id) throws PersistencyException {
		
		EntityManager em = Persistence.createEntityManagerFactory(
				PERSISTENCE_UNIT_NAME).createEntityManager();
		EntityTransaction tx = em.getTransaction();
		try {
			tx.begin();
			Customer gObject = em.find(Customer.class, id);
			tx.commit();
			return gObject;
		  } catch (Exception ex) {
				if (tx != null && tx.isActive()) {
					tx.rollback();
				}
				throw new PersistencyException("Could not perform action.Rollback triggered!", ex);
		  } finally {
				if (em != null && em.isOpen()) {
					em.close();
				}
		  }
	}


	@Override
	public int getCount() throws PersistencyException {
		EntityManager em = Persistence.createEntityManagerFactory(
				PERSISTENCE_UNIT_NAME).createEntityManager();
		EntityTransaction tx = em.getTransaction();
		try {
			tx.begin();
			int pCount = Integer.parseInt(em
					.createQuery("Select count(cust.customerId) from Customer cust")
					.getSingleResult().toString());
			tx.commit();
			return pCount;
		  } catch (Exception ex) {
				if (tx != null && tx.isActive()) {
					tx.rollback();
				}
				throw new PersistencyException("Could not perform action.Rollback triggered!", ex);
		  } finally {
				if (em != null && em.isOpen()) {
					em.close();
				}
		  }
	}
	
	@SuppressWarnings("unchecked")
	public List<Customer> getAllActive() throws PersistencyException {
		EntityManager em = Persistence.createEntityManagerFactory(
				PERSISTENCE_UNIT_NAME).createEntityManager();
		EntityTransaction tx = em.getTransaction();
		try {
			tx.begin();
			List<Customer> dObjects = em.createQuery(
					"Select cust from Customer cust where active = true")
					.getResultList();
			tx.commit();
			return dObjects;
		  } catch (Exception ex) {
				if (tx != null && tx.isActive()) {
					tx.rollback();
				}
				throw new PersistencyException("Could not perform action.Rollback triggered!", ex);
		  } finally {
				if (em != null && em.isOpen()) {
					em.close();
				}
		  }
	}
}
